<?php

namespace IDock\Insurance\TPIC;

use IDock\Common\BaseInterface\BuilderInterface;
use IDock\Common\Tools\LoopXml;
use IDock\Common\Tools\XMLTools;
use IDock\Http\Request;
use IDock\Http\Response;
use IDock\Insurance\TPIC\Request\AllTPICRequest;
use IDock\Insurance\TPIC\Response\AllTPICResponse;

class Builder implements BuilderInterface
{

    public function getConfig(): array
    {
        return $this->config;
    }

    protected $ip;

    protected $config;

    protected $chalCode;

    protected $userCode;

    protected $productCode;

    protected $privateKey;

    protected $clientName;
    protected $publicKeyTP;

    public function execute(Request $request): Response
    {
        /*** @var AllTPICRequest $request */
        $request->loadConfig($this->config);

        $request->setRequestId(md5(uniqid()));
        $request->route = $this->clientName . '/' . $request->route;
        $data = $this->generate($request);
        $param = [
            'head' => [
                'msgId' => $request->getRequestId(),
                'timeStamp' => date('Y-m-d H:i:s'),
                'session' => '',
                'userCode' => substr('hxy' . sha1(uniqid()),0,40),
                'clientName' => $this->clientName,
            ],
            'body' => $data
        ];

        $xml = LoopXml::ArrayToXml($param, 'request');
        $xml = '<?xml version="1.0" encoding="UTF-8"?>' . $xml;
        $request->setPreParameter($xml);
        $request->setBizParam($xml);
        $request = $this->sign($request);

        return $this->request($request);
    }

    public function generate(Request $request): array
    {
        return $request->generate();
    }

    public function sign(Request $request): Request
    {
        $algo = 'SHA256';
        $privateKey = openssl_pkey_get_private($this->getPrivateKey($this->privateKey));
        openssl_sign($request->getBizParam(), $binarySign, $privateKey, $algo);
        $sign = base64_encode($binarySign);
        $request->setHeaders([
            'TP-SIGN' => $sign,
            'Content-Type' => 'application/xml'
        ]);
        return $request;
    }

    private function getPrivateKey($pKsy)
    {
        $str = chunk_split($pKsy, 64);
        return "-----BEGIN RSA PRIVATE KEY-----\r\n" . $str . "-----END RSA PRIVATE KEY-----";
    }


    public function exceptionResponse($request, $errorMessage): Response
    {
        $response = $request->getResponse();

        $response = new $response;
        $response->setCode('500');
        $response->setError($errorMessage);

        return $response;
    }

    public function request(Request $request): Response
    {
        /*** @var AllTPICResponse $response */
        /*** @var AllTPICRequest $request */
        $response = $request->request($request);
        $response->setVisitor($request->getVisitor());

        $request->responseLogsTPIC($request->getRequestId(),$response->contents,$response->isSuccess(),$request->getBizParam());

        if($response->isSuccess()){
            $response->printerInit();
        }

        return $response;
    }

    /**
     * 数据加密
     * @param Request $request
     */
    public function encrypt(Request $request)
    {

    }

}


